package uw.illno.controller


import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import uw.illno.service.AdministratorService
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import uw.illno.pojo.Admin
import uw.illno.pojo.Student
import uw.illno.pojo.Teacher

@Controller
class UserLogin {
    @Autowired
    private lateinit var service: AdministratorService

    @RequestMapping(value = ["/login"], method = [RequestMethod.POST])
    fun check(uno: String, pwd: String, model: Model, user: String): String {
        if (uno == "" || pwd == "") {
            model.addAttribute("msg", "用户名或密码不得为空！")
            return "login"
        }
        when (user) {
            "admin" -> {
                val admin = Admin()
                admin.ano = uno
                return when(service.acheck(admin)) {
                     null -> {
                        model.addAttribute("msg", "该管理员不存在！")
                        "login"
                    }
                    pwd -> "/adpages/adminindex"
                    else -> {
                        model.addAttribute("msg", "密码错误，爬！")
                        "login"
                    }
                }
            }
            "student" -> {
                val student = Student()
                student.sno = uno
                return when (service.scheck(student)) {
                    null -> {
                        model.addAttribute("msg", "该学生不存在！")
                        "login"
                    }
                    pwd -> "/studentpages/studentindex"
                    else -> {
                        model.addAttribute("msg", "密码错误，爬！")
                        "login"
                    }
                }
            }
            "teacher" -> {
                val teacher = Teacher()
                teacher.tno = uno
                return when (service.tcheck(teacher)) {
                    null -> {
                        model.addAttribute("msg", "该教师不存在！")
                        "login"
                    }
                    pwd -> "/teacherpages/teacherindex"
                    else -> {
                        model.addAttribute("msg", "密码错误，爬！")
                        "login"
                    }
                }
            }
            else -> return "login"
        }
    }
}

